<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .root{
            width: 200px;
            height: 200px;
            background: blueviolet;
        }
        .box{
            width: 100px;
            height: 100px;
            background: lightpink;
        }
    </style>
</head>
<body>
    <div class="root" id="root">
        <div class="box" id="box">
        </div>
    </div>
    <script>
        var root = document.getElementById('root')
        var box = document.getElementById('box')

        root.addEventListener('click',function(){
            alert('点了父容器')
        })

        box.addEventListener('click',function(e){
            alert('点了子容器')
            e.stopPropagation()//阻止冒泡事件，没有这一段就会在点击了子容器时同时触发点击父容器的方法
        })
    </script>
</body>
</html>